<?php
require_once '../../Lib/main.php';
$id 			= null;
$name 			= null;
$id_category 	= null;
$id_prodclass 	= null;
$originPlace 	= null;
$productYear 	= null;

$capacity 		= null;
$level 			= null;
$price 			= null;
$oldPrice 		= null;
$detail 		= null;
$viewTimes 		= null;
$deleteFLg 		= null;
if (isset($_REQUEST['id']) && trim($_REQUEST['id']) !== '') {
	$id = $_REQUEST['id'];
}

if (isset($_REQUEST['name']) && trim($_REQUEST['name']) !== '') {
	$name = $_REQUEST['name'];
}

if (isset($_REQUEST['id_category']) && trim($_REQUEST['id_category']) !== '') {
	$id_category = $_REQUEST['id_category'];
}

if (isset($_REQUEST['id_prodclass']) && trim($_REQUEST['id_prodclass']) !== '') {
	$id_prodclass = $_REQUEST['id_prodclass'];
}

if (isset($_REQUEST['originPlace']) && trim($_REQUEST['originPlace']) !== '') {
	$originPlace = $_REQUEST['originPlace'];
}

if (isset($_REQUEST['productYear']) && trim($_REQUEST['productYear']) !== '') {
	$productYear = $_REQUEST['productYear'];
}

if (isset($_REQUEST['capacity']) && trim($_REQUEST['capacity']) !== '') {
	$capacity = $_REQUEST['capacity'];
}

if (isset($_REQUEST['level']) && trim($_REQUEST['level']) !== '') {
	$level = $_REQUEST['level'];
}

if (isset($_REQUEST['price']) && trim($_REQUEST['price']) !== '') {
	$price = $_REQUEST['price'];
}

if (isset($_REQUEST['oldPrice']) && trim($_REQUEST['oldPrice']) !== '') {
	$oldPrice = $_REQUEST['oldPrice'];
}

if (isset($_REQUEST['detail']) && trim($_REQUEST['detail']) !== '') {
	$detail = $_REQUEST['detail'];
}

if (isset($_REQUEST['viewTimes']) && trim($_REQUEST['viewTimes']) !== '') {
	$viewTimes = $_REQUEST['viewTimes'];
}

if (isset($_REQUEST['deleteFLg']) && trim($_REQUEST['deleteFLg']) !== '') {
	$deleteFLg = $_REQUEST['deleteFLg'];
}

$productDao = new  Dao_ProductDAO();
$product = null;
if ($id === null) {
	$product = new Model_Product(array());
} else {
	$product = $productDao->getProductObjById($id);
}
$product->setId($id);
$product->setName($name);

$product->setIdCategory($id_category);
$product->setIdProductClass($id_prodclass);
$product->setOrigin($originPlace);
$product->setYearOfManufacture($productYear);

$product->setCapacity($capacity);
$product->setLevel($level);
$product->setPrice($price);
$product->setOldPrice($oldPrice);
$product->setDetail($detail);
$product->setViewTimes($viewTimes);
$product->setDeleteFlg($deleteFLg);

$success = $productDao->insertOrUpdate($product);

$url = '';
if($success) {
	if ($id === null ||trim($id) == '' ) {
		$id = $productDao->getMaxId();
	}
	$message = "";
	if ($_FILES["imageFile"]["name"] !== null && $_FILES["imageFile"]["name"] !== '') {
		$allowedExts = array("gif", "jpeg", "jpg", "png");
		$temp = explode(".", $_FILES["imageFile"]["name"]);
		$extension = end($temp);
		$uploadSuccess = false;

		if ((($_FILES["imageFile"]["type"] == "image/gif")
				|| ($_FILES["imageFile"]["type"] == "image/jpeg")
				|| ($_FILES["imageFile"]["type"] == "image/jpg")
				|| ($_FILES["imageFile"]["type"] == "image/pjpeg")
				|| ($_FILES["imageFile"]["type"] == "image/x-png")
				|| ($_FILES["imageFile"]["type"] == "image/png"))
				&& ($_FILES["imageFile"]["size"] < 3145728) // 3MB
				&& in_array($extension, $allowedExts)) {
			if ($_FILES["imageFile"]["error"] > 0) {
				//echo "Error: " . $_FILES["imageFile"]["error"] . "<br>";
				$uploadSuccess = false;
				$message = "file bị lỗi (error = " . $_FILES["imageFile"]["error"] .")";
			} else {
	// 			echo "Upload: " . $_FILES["imageFile"]["name"] . "<br>";
	// 			echo "Type: " . $_FILES["imageFile"]["type"] . "<br>";
	// 			echo "Size: " . ($_FILES["imageFile"]["size"] / 1024) . " kB<br>";
	// 			echo "Stored in: " . $_FILES["imageFile"]["tmp_name"];

				$path = __DIR__ . "/../../". PICTURE_PROD_FOLDER. "/" . $product->getNameClassFolder();
				if (!file_exists($path)) {
					mkdir($path, 0777);
				}

				$ext = pathinfo($_FILES["imageFile"]["name"], PATHINFO_EXTENSION);

				if ($product->getImageUrl() !== null && trim($product->getImageUrl()) !== "") {
					if (file_exists($path . "/" . $product->getImageUrl())) {
						unlink($path . "/" . $product->getImageUrl());
					}
				}

				move_uploaded_file($_FILES["imageFile"]["tmp_name"],
											$path . "/" . $id. "." . $ext);
				$uploadSuccess = true;
				$message = "";
				if ($product->getId() === null || trim($product->getId()) === '' ) {
					$product->setId($id);
				}
				$product->setImageUrl($id. "." . $ext);
				$productDao->insertOrUpdate($product);
				$message = "upload hình thành công ";
			}
		} else {
			//echo "Invalid file";
			$uploadSuccess = false;
			$message = "Không thực hiện upload file vì file không phải là file hình";
		}
	}

	$url = getUrlAdmin("success.php", "rep=listproduct.php&m=$message");

} else {
	$url = getUrlAdmin("error.php", "rep=listproduct.php&m=$message");
}

//header('Location: ../'.$url);
?>
<script type="text/javascript" src="../js/custom.js"></script>
<script type="text/javascript">
 	goTo('../<?php echo $url;?>');

</script>
<?php echo $url;?>